import requests
import json
# 读取cityid.txt文件内容
sheng=""
shi=""
xian=""
with open('/home/pi-star/mmdvm-wendu/cityid.txt', 'r', encoding='GBK') as f:
    sheng = f.readline().strip() # 读取第一行(省)
    shi = f.readline().strip() # 读取第二行(市)
    xian = f.readline().strip() # 读取第三行(县)
# 打印读取到的内容
   # print("省：%s" % sheng)
    #print("市：%s" % shi)
    #print("县：%s" % xian)
    #with open('weather.txt', 'w', encoding='utf-8') as f:
        #f.write("%s\n"% sheng)
        #f.write("%s\n"% shi)
        #f.write("%s\n"% xian)
url_template = "https://wis.qq.com/weather/common?source=pc&weather_type=forecast_24h&province={}&city={}&county={}"
url = url_template.format(sheng, shi, xian)
#print(url)
response = requests.get(url)
#print(response)
if response.status_code == 200:
    try:
        weather_data=response.json()
       # print(weather_data)
        
        yestody = weather_data['data']['forecast_24h']['0'] 
        today = weather_data['data']['forecast_24h']['1']
        tomorrow = weather_data['data']['forecast_24h']['2']
        tomorrow1 = weather_data['data']['forecast_24h']['3']
        tomorrow2 = weather_data['data']['forecast_24h']['4']
        tomorrow3 = weather_data['data']['forecast_24h']['5']
        tomorrow4 = weather_data['data']['forecast_24h']['6']
        tomorrow5 = weather_data['data']['forecast_24h']['7']
                   
      # 使用Python 3.4的字符串格式化
      #0
        yestody_w = "{time}白天{day_weather},最高温度{max}℃,{wind}{power}级,夜间{night_weather},最低温度{min}℃,{night_wind_direction}{night_wind_power}级".format(
        time=yestody['time'],
        day_weather=yestody['day_weather'],
        max=yestody['max_degree'],
        wind=yestody['day_wind_direction'],
        power=yestody['day_wind_power'], 
        night_weather=yestody['night_weather'],
        min=today['min_degree'],
        night_wind_direction=yestody['night_wind_direction'],
        night_wind_power=yestody['night_wind_power'] )
        print(yestody_w)
     # 1          
        today_w = "{time}白天{day_weather},最高温度{max}℃,{wind}{power}级,夜间{night_weather},最低温度{min}℃,{night_wind_direction}{night_wind_power}级".format(
        time=today['time'],
        day_weather=today['day_weather'],
        max=today['max_degree'],
        wind=today['day_wind_direction'],
        power=today['day_wind_power'], 
        night_weather=today['night_weather'],
        min=today['min_degree'],
        night_wind_direction=today['night_wind_direction'],
        night_wind_power=today['night_wind_power'] )
        print(today_w)
     #2
        tomorrow_w = "{time}白天{day_weather},最高温度{max}℃,{wind}{power}级,夜间{night_weather},最低温度{min}℃,{night_wind_direction}{night_wind_power}级".format(
        time=tomorrow['time'],
        day_weather=tomorrow['day_weather'],
        max=tomorrow['max_degree'],
        wind=tomorrow['day_wind_direction'],
        power=tomorrow['day_wind_power'], 
        night_weather=tomorrow['night_weather'],
        min=tomorrow['min_degree'],
        night_wind_direction=tomorrow['night_wind_direction'],
        night_wind_power=tomorrow['night_wind_power'] )
        print(tomorrow_w)
     #3
        tomorrow1_w = "{time}白天{day_weather},最高温度{max}℃,{wind}{power}级,夜间{night_weather},最低温度{min}℃,{night_wind_direction}{night_wind_power}级".format(
        time=tomorrow1['time'],
        day_weather=tomorrow1['day_weather'],
        max=tomorrow1['max_degree'],
        wind=tomorrow1['day_wind_direction'],
        power=tomorrow1['day_wind_power'], 
        night_weather=tomorrow1['night_weather'],
        min=tomorrow1['min_degree'],
        night_wind_direction=tomorrow1['night_wind_direction'],
        night_wind_power=tomorrow1['night_wind_power'] )
        print(tomorrow1_w)
     #4
        tomorrow2_w = "{time}白天{day_weather},最高温度{max}℃,{wind}{power}级,夜间{night_weather},最低温度{min}℃,{night_wind_direction}{night_wind_power}级".format(
        time=tomorrow2['time'],
        day_weather=tomorrow2['day_weather'],
        max=tomorrow2['max_degree'],
        wind=tomorrow2['day_wind_direction'],
        power=tomorrow2['day_wind_power'], 
        night_weather=tomorrow2['night_weather'],
        min=tomorrow2['min_degree'],
        night_wind_direction=tomorrow2['night_wind_direction'],
        night_wind_power=tomorrow2['night_wind_power'] )
        print(tomorrow2_w)
     #5
        tomorrow3_w = "{time}白天{day_weather},最高温度{max}℃,{wind}{power}级,夜间{night_weather},最低温度{min}℃,{night_wind_direction}{night_wind_power}级".format(
        time=tomorrow3['time'],
        day_weather=tomorrow3['day_weather'],
        max=tomorrow3['max_degree'],
        wind=tomorrow3['day_wind_direction'],
        power=tomorrow3['day_wind_power'], 
        night_weather=tomorrow3['night_weather'],
        min=tomorrow3['min_degree'],
        night_wind_direction=tomorrow3['night_wind_direction'],
        night_wind_power=tomorrow3['night_wind_power'] )
        print(tomorrow3_w)
     #6
        tomorrow4_w = "{time}白天{day_weather},最高温度{max}℃,{wind}{power}级,夜间{night_weather},最低温度{min}℃,{night_wind_direction}{night_wind_power}级".format(
        time=tomorrow4['time'],
        day_weather=tomorrow4['day_weather'],
        max=tomorrow4['max_degree'],
        wind=tomorrow4['day_wind_direction'],
        power=tomorrow4['day_wind_power'], 
        night_weather=tomorrow4['night_weather'],
        min=tomorrow4['min_degree'],
        night_wind_direction=tomorrow4['night_wind_direction'],
        night_wind_power=tomorrow4['night_wind_power'] )
        print(tomorrow4_w)
     #7
        tomorrow5_w = "{time}白天{day_weather},最高温度{max}℃,{wind}{power}级,夜间{night_weather},最低温度{min}℃,{night_wind_direction}{night_wind_power}级".format(
        time=tomorrow5['time'],
        day_weather=tomorrow5['day_weather'],
        max=tomorrow5['max_degree'],
        wind=tomorrow5['day_wind_direction'],
        power=tomorrow5['day_wind_power'], 
        night_weather=tomorrow5['night_weather'],
        min=tomorrow5['min_degree'],
        night_wind_direction=tomorrow5['night_wind_direction'],
        night_wind_power=tomorrow5['night_wind_power'] )
        print(tomorrow5_w)
                   
        with open('/home/pi-star/mmdvm-wendu/weather_forecast_7day.txt', 'w', encoding='utf-8') as file:
            file.write(yestody_w+'\n'+today_w+'\n'+tomorrow_w+'\n'+tomorrow1_w+'\n'+tomorrow2_w+'\n'+tomorrow3_w+'\n'+tomorrow4_w+'\n'+tomorrow5_w)        
        print("天气数据已写入weather_forecast_7day.txt文件。")
    except ValueError:
        print("响应数据不是有效的JSON格式。")
else:
    print("请求失败，状态码: {response.status_code}")




